package live.jialing.sip;

import live.jialing.data.jpa.RepositoryFactoryBean;
import live.jialing.platform.context.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@SpringBootApplication
@ComponentScan(basePackages = "live.jialing")
@EnableTransactionManagement
// JPA
@EntityScan(basePackages = { "live.jialing.**.domain" })
@EnableJpaRepositories(basePackages = {
        "live.jialing.**.repository"}, transactionManagerRef = "transactionManager", entityManagerFactoryRef = "entityManagerFactory", repositoryFactoryBeanClass = RepositoryFactoryBean.class, repositoryImplementationPostfix = "CustomImpl")
// Mybatis
//@MapperScan("live.jialing.**.repository.mapper")
public class Application extends SpringBootServletInitializer {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(Application.class);
    }

    @Override
    protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {

        WebApplicationContext wac = super.createRootApplicationContext(servletContext);
        return wac;
    }

    public static void main(String[] args) throws Exception {

        SpringApplication app = new SpringApplication(Application.class);
        ApplicationContext ctx = app.run(args);

        SpringContextUtil.setApplicationContext(ctx);

        logger.debug("启动成功 : "+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}
